﻿using System;
using System.Collections.Generic;
using System.Text;

/*
	Todo extract some base functionality
	06/07/2012: Added quickdiff to the Top left menu.
*/
namespace SqliteDriver
{
	class SqlServerPopupBuilder
	{
		public SqlServerPopupBuilder()
		{
			this.IsDevelopmentDatabase = false;
		}
		public SqlServerPopupBuilder(bool isDevDb)
		{
			this.IsDevelopmentDatabase = isDevDb;
		}

		bool IsDevelopmentDatabase {get; set; }

        // standards used by this and broker
		public static DbViewDriver.CustomMenuItem AddItem(DbViewDriver.CustomMenuItem root, String menuText, DbView.Commands.Cmnd command)
        {
			DbViewDriver.CustomMenuItem newOne = new DbViewDriver.CustomMenuItem(DbViewDriver.CustomMenuItem.ItemType.item, menuText, command.ToString());
            root.AddItem(AddHelp(newOne, new DbView.Command(command)));
            return newOne;
        }

		public static DbViewDriver.CustomMenuItem AddHelp(DbViewDriver.CustomMenuItem item, DbView.Command command)
		{
			String help = DbView.CommandHelpRepository.Instance.GetHelp(command);
			item.Help = help;
            return item;
        }

		public DbViewDriver.CustomMenuItem AddMenu(DbViewDriver.CustomMenuItem root, String menuText)
        {
			DbViewDriver.CustomMenuItem newOne = new DbViewDriver.CustomMenuItem(DbViewDriver.CustomMenuItem.ItemType.menu, menuText);
            root.AddItem(newOne);
            return newOne;
        }

		public static DbViewDriver.CustomMenuItem AddCustomItem(DbViewDriver.CustomMenuItem root, String menuText, DbView.Command command)
        {
			DbViewDriver.CustomMenuItem newOne = new DbViewDriver.CustomMenuItem(DbViewDriver.CustomMenuItem.ItemType.item, menuText, command.ToXml());
            root.AddItem(AddHelp(newOne, command));
            return newOne;
        }
        public static void AddSeparator(DbViewDriver.CustomMenuItem root)
        {
            root.AddItem(new DbViewDriver.CustomMenuItem(DbViewDriver.CustomMenuItem.ItemType.separator));
        }
		public static DbViewDriver.CustomMenuItem AddCustomMenuItem(DbViewDriver.CustomMenuItem menu, String text, String commandTag)
		{
			DbView.Command c = Cmd.MakeCommand(commandTag);
			DbViewDriver.CustomMenuItem newOne = new DbViewDriver.CustomMenuItem(DbViewDriver.CustomMenuItem.ItemType.item, text, c.ToXml());
			menu.AddItem(AddHelp(newOne, c));
			return newOne;
		}

		public static DbViewDriver.CustomMenuItem AddCustomMenuItem(DbViewDriver.CustomMenuItem menu, String text, String commandTag, int extraColumnNumber)
		{
			DbView.Command c = Cmd.MakeCommand(commandTag);
			DbViewDriver.CustomMenuItem newOne = new DbViewDriver.CustomMenuItem(DbViewDriver.CustomMenuItem.ItemType.item, text, c.ToXml());
			menu.AddItem(AddHelp(newOne, c));
			return newOne;
		}

		public static void AddProfile(DbViewDriver.CustomMenuItem root) { AddItem(root, "Profile", DbView.Commands.Cmnd.Profile); }
        public static void AddShowSql(DbViewDriver.CustomMenuItem root) // needs renaming
        {
			AddExportToExcel(root);
			DbViewDriver.CustomMenuItem item = AddItem(root, "Show Sql", DbView.Commands.Cmnd.ShowCommand);
			item.IsRowIndependent = true;
		}
        public static void AddExportToExcel(DbViewDriver.CustomMenuItem root)
        {
			DbViewDriver.CustomMenuItem item = AddItem(root, "Grid to Excel", DbView.Commands.Cmnd.Excel);
			item.IsRowIndependent = true;
		}
        /*
            standard popup offering of profile/show sql until you think of what else 
            to implement as a popup
        */
        public static void AppendDefault(DbViewDriver.CustomMenuItem root, bool withSeparator)
        {
            if (withSeparator) AddSeparator(root);
            AddProfile(root);
            AddShowSql(root);
        }
        
        public DbViewDriver.CustomMenuItem BuildPopupMenu(DbView.Command command)
		{
			DbViewDriver.CustomMenuItem root = new DbViewDriver.CustomMenuItem(DbViewDriver.CustomMenuItem.ItemType.menu);
			DbViewDriver.CustomMenuItem submenu;
			// [Specialise: popup menus]
			switch (command.Cmd)
			{
				case DbView.Commands.Cmnd.Tables:
					AddItem(root, "Data", DbView.Commands.Cmnd.TableData);
					AddItem(root, "Fields", DbView.Commands.Cmnd.Fields);
					if (this.IsDevelopmentDatabase)
					{
						AddSeparator(root);
						submenu = this.AddMenu(root, "Modify");
						AddItem(submenu, "Truncate [ ]", DbView.Commands.Cmnd.Cleartable);
						AddItem(submenu, "Drop [ ]", DbView.Commands.Cmnd.Droptable);
						AddCustomMenuItem(submenu, "Reset AutoIncrement", Cmd.ResetAuto);
					}
					AddSeparator(root);
					AddItem(root, "Find in Source", DbView.Commands.Cmnd.SearchSource);
					AddSeparator(root);
					AddCustomMenuItem(root, "Script POCO Class", Cmd.ScriptClass);
					AddItem(root, "Export", DbView.Commands.Cmnd.Export);
					AddItem(root, "Import", DbView.Commands.Cmnd.Import);
					AddSeparator(root);
					AddCustomMenuItem(root, "New Table",Cmd.CreateTable).IsRowIndependent = true;
					AddShowSql(root);
					break;
				case DbView.Commands.Cmnd.TableData:
					AddItem(root, "Profile All", DbView.Commands.Cmnd.Profile);
					this.AddProfileDataOnly(root);
					AddItem(root, "Fields", DbView.Commands.Cmnd.Fields).IsRowIndependent = true;
					AddItem(root, "Edit View", DbView.Commands.Cmnd.EditView).IsRowIndependent = true;
					this.AddScriptItem(root, "Script Data", "DATA").IsRowIndependent = true;
					AddItem(root, "Export", DbView.Commands.Cmnd.Export).IsRowIndependent = true; 
					if (this.IsDevelopmentDatabase)
					{
						AddSeparator(root);
						AddItem(root, "Delete All Data", DbView.Commands.Cmnd.Cleartable).IsRowIndependent = true;
					}
					AddSeparator(root);
					AddShowSql(root);
					break;
				case DbView.Commands.Cmnd.Fields:
					AddProfile(root);
					AddItem(root, "Find in Source", DbView.Commands.Cmnd.SearchSource);
					if (this.IsDevelopmentDatabase)
					{
						AddItem(root, "Modify", DbView.Commands.Cmnd.ChangeColumn);
						AddItem(root, "Remove", DbView.Commands.Cmnd.RemoveColumn);
						AddSeparator(root);
						AddItem(root, "Add New Field", DbView.Commands.Cmnd.AddColumn).IsRowIndependent = true;
					}
					AddSeparator(root);
					AddShowSql(root);
					break;

				case DbView.Commands.Cmnd.Views:
					AddItem(root, "View Source", DbView.Commands.Cmnd.ViewSource);
					AddItem(root, "Data", DbView.Commands.Cmnd.TableData);
					AddItem(root, "Fields", DbView.Commands.Cmnd.Fields);
					AddSeparator(root);
					AddItem(root, "Find In Source", DbView.Commands.Cmnd.SearchSource);
					AddShowSql(root);
					break;
				case DbView.Commands.Cmnd.Procedures:
					AddItem(root, "View Source", DbView.Commands.Cmnd.ShowSource);
					AddItem(root, "Dependencies", DbView.Commands.Cmnd.Dependencies);
					AddShowSql(root);
					break;
				case DbView.Commands.Cmnd.Functions:
					AddItem(root, "View Source", DbView.Commands.Cmnd.ShowSource);
					AddItem(root, "Dependencies", DbView.Commands.Cmnd.Dependencies);
					AddShowSql(root);
					break;
				case DbView.Commands.Cmnd.Triggers:
					AddItem(root, "View Source", DbView.Commands.Cmnd.ShowSource);
					AddSeparator(root);
					AddShowSql(root);
					break;
				case DbView.Commands.Cmnd.Aggregate:
					this.AddAggregateShowTableData(root, "Show Data");
					AppendDefault(root, true);
					break;
				case DbView.Commands.Cmnd.ViewSourceSearch:
					AddItem(root, "View Source", DbView.Commands.Cmnd.ShowSource);
					AddSeparator(root);
					AddShowSql(root);
					break;
				case DbView.Commands.Cmnd.TableFieldSearch:
					AddItem(root, "Fields", DbView.Commands.Cmnd.Fields);
					AddSeparator(root);
					AddShowSql(root);
					break;
				case DbView.Commands.Cmnd.AllSourceSearch:
					AddItem(root, "View Full Source", DbView.Commands.Cmnd.ShowSource);
					AddSeparator(root);
					AddShowSql(root);
					break;
				case DbView.Commands.Cmnd.ForeignKeys:
					AddProfile(root);
					AddShowSql(root);
					break;
				default:
					AppendDefault(root, false);
					break;
			}
			AddHelp(root);
			return root;
		}

		private bool IsColumnDescriptionProperties(DbView.Command command)
		{
			// param 0 must be COLUMN
			String itemType = command[DbView.Command.TableParameterName(0)]; 
			return (itemType == "COLUMN") ? true : false;
		}

		private bool IsTableDescriptionProperties(DbView.Command command)
		{
			// param 0 must be USER_TABLE
			String itemType = command[DbView.Command.TableParameterName(0)]; 
			return (itemType == "USER_TABLE") ? true : false;
		}
		private bool IsMSDescriptionProperty(DbView.Command command)
		{
			String propertyName = command["PROP"]; 
			return (propertyName == "MS_Description") ? true : false;
		}

		private void AddHelp(DbViewDriver.CustomMenuItem root)
		{
		}
		
		/*
			Custom construction
		*/ 
		void AddProfileDataOnly(DbViewDriver.CustomMenuItem root)
		{
			DbView.Command command = new DbView.Command(DbView.Commands.Cmnd.Profile);
			command.AddParam("NOTEMPTY", "Y");
			AddCustomItem(root, "Profile Data only", command);
		}
		
		DbViewDriver.CustomMenuItem AddScriptItem(DbViewDriver.CustomMenuItem root, String menuText, String objectType)
		{
			DbView.Command command = new DbView.Command(DbView.Commands.Cmnd.ShowSource);
			command.AddParam("TYPE", objectType);
			return AddCustomItem(root, menuText, command);
		}

		void AddShowData(DbViewDriver.CustomMenuItem root, String menuText, int targetColumn)
		{
			DbView.Command command = new DbView.Command(DbView.Commands.Cmnd.ShowProfileData, "", DbView.Command.CommandAction.sysaction);
			command.AddParam("DATACOL", targetColumn.ToString());
		}

		void AddAggregateShowTableData(DbViewDriver.CustomMenuItem root, String menuText)
		{
			DbView.Command command = new DbView.Command(DbView.Commands.Cmnd.TableData);
			command.AddParam("CALLER", DbView.Commands.Cmnd.Aggregate.ToString());
			AddCustomItem(root, menuText, command);
		}

		// todo: convert these to Cmd. Strings.
		public DbViewDriver.CustomMenuItem BuildCustomMenu()
		{
			DbViewDriver.CustomMenuItem menu = new DbViewDriver.CustomMenuItem(DbViewDriver.CustomMenuItem.ItemType.menu, "Sqlite", "");
			AddCustomMenuItem(menu, "Integrity", Cmd.PragmaIntegrity);
			AddCustomMenuItem(menu, "Encoding", Cmd.Encoding);

			return menu;
		}
	}
}
